//
//  CityPicker.m
//  YaoHeAPP
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 mac. All rights reserved.
//

//**************************************************本页面用来查询城市

#import "CityPicker.h"
#import "DatabaseTool.h"
#import "city.h"
#import "sheng.h"
@implementation CityPicker
#define App_Frame_Height        [UIScreen mainScreen].bounds.size.height
#define App_Frame_Width         [[UIScreen mainScreen] bounds].size.width
#define Line_Low_Color   [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1]
//红色
#define  RedColor  [UIColor colorWithRed:216.0/255.0 green:12.0/255.0 blue:24.0/255.0 alpha:1]


+(NSString*)returnStrView:(UIView*)selfView  withTitle:(NSString*)titleString
{
    __block  NSString*shengCity=[[NSString alloc]init];
    [self showPickerView:selfView withTitle:titleString andSelectBlock:^(NSString*str) {
        shengCity=str;
    }];
    return shengCity;
}

+(void)showPickerView:(UIView *)selfView withTitle:(NSString *)titleString andSelectBlock:(void (^)(NSString *))selectRow
{
    //创建pickerView
    CityPicker*pickerView=[[CityPicker alloc]initWithFrame:selfView.bounds];
    pickerView.SelectBlock=selectRow;
    pickerView.titleStr=titleString;
    [pickerView setSubviews];
    [selfView addSubview:pickerView];
}
//初始化大小
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfAction)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self performSelector:@selector(showSelfAction) withObject:nil afterDelay:0];
    return self;
}

//设置，要弹出的视图上显示的东西
-(void)setSubviews
{
    //    str1=[NSString string];
    //    str2=[NSString string];
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, App_Frame_Width, 260)];
    _bgView.backgroundColor = Line_Low_Color;
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = .3;
    _bgView.layer.shadowOffset = CGSizeMake(0, -1);
    [self addSubview:_bgView];
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake((App_Frame_Width - 140)/2, 0, 140, 40)];
    titleL.backgroundColor = [UIColor clearColor];
    titleL.font = [UIFont systemFontOfSize:17];
    titleL.textColor = [UIColor whiteColor];
    titleL.text = self.titleStr;
    
    [_bgView addSubview:titleL];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.backgroundColor = [UIColor clearColor];
    confirmBtn.layer.cornerRadius = 4;
    confirmBtn.layer.borderWidth = 0.5;
    confirmBtn.layer.borderColor = RedColor.CGColor;
    confirmBtn.frame = CGRectMake(App_Frame_Width - 105, 7, 90, 32);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:RedColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:confirmBtn];
    
    
    
    _shengCitydic=[[NSMutableDictionary alloc]init];
    _shengArray=[NSMutableArray array];
    _cityArray=[NSMutableArray array];
    _shengArray=[DatabaseTool querySheng];
    NSArray*arrayCity=[DatabaseTool queryCity];
    
    NSString *shengid;//省ID、根据省ID找出对应的市，添加到数组中
    //遍历数组
    for (NSInteger i=0; i<arrayCity.count; i++)
    {
        city*city=arrayCity[i];
        shengid=city.shengID;
        if([[_shengCitydic allKeys] containsObject:shengid])
        {
            NSMutableArray*citys=[_shengCitydic valueForKey:shengid];
            [citys addObject:city];
        }
        else
        {
            NSMutableArray*citys=[NSMutableArray array];
            [citys addObject:city];
            [_shengCitydic setObject:citys forKey:shengid];
        }
    }
    sheng*title=_shengArray[0];
    [_cityArray  addObjectsFromArray:_shengCitydic[title.shengID]];
    
    //初始化pickerView，
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, App_Frame_Width, 216)];
    _pickerView.backgroundColor = [UIColor whiteColor];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_bgView addSubview:_pickerView];
}

//点击确定按钮执行的方法
- (void)confirmBtnAction
{
    //[_pickerView reloadAllComponents];
    if (sheng_Name==nil)
    {
        sheng*sheng_Ming=_shengArray[0];
        sheng_Name=sheng_Ming.shengName;
        sheng_ID=sheng_Ming.shengID;
    }
    if (city_Name==nil)
    {
        if (_cityArray.count==0)
        {
            return;
        }
        else
        {
            //city_Name=_cityArray[0];
            //NSArray*array=[DatabaseTool queryCityID:city_Name];
            city*city_Ming=_cityArray[0];
            city_Name=city_Ming.cityName;
            city_ID=city_Ming.cityID;
        }
    }
    //NSNumber*aShengID=[NSNumber numberWithInteger:sheng_ID];
    //NSNumber*aCityID=[NSNumber numberWithInteger:city_ID];
    NSString*string=[NSString stringWithFormat:@"%@-%@*%@|%@",sheng_Name,city_Name,sheng_ID,city_ID];
    _stringRow=string;
    self.SelectBlock(_stringRow);
    [self removeSelfAction];
}
//展示出选择器
- (void)showSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0, -260);
    } completion:^(BOOL finished) {
        
    }];
}
//移除选择器
- (void)removeSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform  = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

#pragma mark----------UIPickerView的协议方法
//有几列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0)
    {
        return _shengArray.count;
    }
    return _cityArray.count;
}
//每个地方要显示的文字
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    if (component==0)
    {
        UIView *myView = [[UIView alloc]init];
        myView.frame =CGRectMake(0.0f, 0.0f, pickerView.frame.size.width, 40);
        UILabel *txtlabel = [[UILabel alloc] init];
        txtlabel.textAlignment = NSTextAlignmentCenter;
        txtlabel.frame = myView.frame;
        sheng*title=_shengArray[row];
        txtlabel.text = title.shengName;
        [myView addSubview:txtlabel];
        return myView;
    }
    UIView *myViewt=[[UIView alloc]init];
    myViewt.frame =CGRectMake(0, 0.0f, pickerView.frame.size.width, 40);
    UILabel *txtlabel2 = [[UILabel alloc] init];
    txtlabel2.textAlignment = NSTextAlignmentCenter;
    txtlabel2.frame = myViewt.frame;
    city*title=_cityArray[row];
    txtlabel2.text=title.cityName;
    [myViewt addSubview:txtlabel2];
    return myViewt;
}

//获去当前选中的是哪一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    //cityShengName=[[NSMutableArray alloc]init];
    if (component==0)
    {
        sheng*title=_shengArray[row];
        sheng_Name=title.shengName;
        sheng_ID=title.shengID;
        [_cityArray removeAllObjects];
        [_cityArray  addObjectsFromArray:_shengCitydic[sheng_ID]];
        [pickerView reloadAllComponents];
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
    if (component==1)
    {
        city*title=_cityArray[row];
        city_Name=title.cityName;
        city_ID=title.cityID;
    }
}

#pragma tap  delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_bgView.frame, point)) {
        return NO;
    }
    
    return YES;
}

@end
